package battle.controllers;


import battle.gameplay.Bullet;
import battle.gameplay.GameScene;
import battle.gameplay.Tank;

import com.jme3.bullet.collision.PhysicsCollisionGroupListener;
import com.jme3.bullet.collision.PhysicsCollisionObject;

public class CollisionController implements PhysicsCollisionGroupListener{
	
	private Tank tank;
	private GameScene scene;
	
	private final int group1 = PhysicsCollisionObject.COLLISION_GROUP_01;
	private final int group2 = PhysicsCollisionObject.COLLISION_GROUP_02;
	private final int group3 = PhysicsCollisionObject.COLLISION_GROUP_03;
	
	public void setCollisionComponents(Tank tank, GameScene scene){
		this.tank = tank;
		this.scene = scene;
	}

	public boolean collide(PhysicsCollisionObject nodeA, PhysicsCollisionObject nodeB){
		int groupA = nodeA.getCollisionGroup();
		int groupB = nodeB.getCollisionGroup();
		if (groupA == group1 && groupB == group1)
			tank.newCommand("FastRollBack");
		else if ((groupA == group2 || groupB == group2) && (groupA == group1 || groupB == group1))
			tank.newCommand("FastRollBack");
		else{
			if (groupA == group3){
				if (groupB == group1)
					((Tank) nodeB).newCommand("Hit");
				else
					scene.sceneHit(nodeB, ((Bullet) nodeA).getDirection());
				((Bullet) nodeA).bulletHit();
			}
			else{
				if (groupA == group1)
					((Tank) nodeA).newCommand("Hit");
				else
					scene.sceneHit(nodeA, ((Bullet) nodeB).getDirection());
				((Bullet) nodeB).bulletHit();
			}
		}
		return false;
	}
}

